package com.bart.hsp.scala.ch10.sec03

/**
 * trait 可以有实现方法
 */
trait ConsoleLogger {
  def log(msg: String): Unit = {
    println(f"from ConsoleLogger $msg")
  }
}

class Account {
  protected var balance = 0.0
}

/**
 * 使用混入的方式去实现不是通过重写
 */
class SavingsAccount extends Account with ConsoleLogger {
  def withdraw(amount: Double) {
    if (amount > balance) log("Insufficient funds")
    else balance -= amount
  }

  /**
   * 可以用 override 覆盖，否则就是用父类的方法
   * @param msg
   */
  override def log(msg: String): Unit = {
    println(f"from SavingsAccount $msg")
  }
  // More methods ...
}

object Main extends App {
  val acct = new SavingsAccount
  acct.withdraw(100)
}
